//const router = new require('koa-router')()
const Router =  require('../koa-router-master/lib/router');
const router=new Router();
const fs = require('fs')
const path = require('path');
const tool=require('../tool/tool');
var BaseRequest=require('../mongodb/baserequest.js');
var Model=new BaseRequest('user');


router.prefix('/api/login');


router.use(async(ctx,next)=>{
  ctx.response.type='json';
  await next();
})
router.get('/',async function (ctx, next) {
  await ctx.render('user',{users:[{name:'star'}],user:ctx.session.user || {}})
})
router.post('/upload',async function(ctx,next){
  console.log(ctx.query)
  ctx.body={
    title:ctx.request.body.title,
    picture:ctx.request.picture || [],
    file:ctx.request.file || [],
  }
})
router.get('/set', async (ctx,next)=>{
	const n = Number(ctx.cookies.get('view') || 0) + 1;
    ctx.cookies.set('view', n);
    ctx.response.body = n + ' views';
})
router.get('/session',(ctx,next)=>{
  let n = ctx.session.views || 0;
  ctx.session.views = ++n;
  ctx.body = n + ' views';
})
router.get('/code',(ctx,next)=>{
  ctx.response.status=406;
  //throw new Error('fdafa')//throw 之后的代码不会执行
  ctx.throw(405,'333')
})
router.post('/login',async function(ctx,next){
  var req=ctx.request;
  //console.log(req.header.cookie,req.body);
  var data={message:'用户名密码错误',errorCode:10}
  var result=await Model.find({name:req.body.username,pwd:req.body.password});
  //console.log(result);
  if(result.errorCode==0){
      var doc=result.data;
      if(doc.length>0){
          if(doc[0].state=="1"){
              data.errorCode=0;
              data.message='';
              
              data.data=JSON.parse(JSON.stringify(doc[0]));
              var id=data.data._id
              delete data.data.pwd;
              delete data.data._id;
              delete data.data.__v;   
              data.data.realName=data.data.name;
              data.data.token='34214321';
              var d=new Date();
              d.setDate(d.getDate()+1);
              
              data.data.expDate=tool.format(d,'yyyy-MM-dd hh:mm:ss');
              ctx.session.user=data.data;
              ctx.session.user._id=id;
          }else{
              data.message='此用户已被禁用'
          }
          
      }
      //console.log(data,ctx.session.user);
      ctx.body=data;
  }else{
      ctx.body=result;
  }
})
router.post('/signout',function(ctx,next){
  if(ctx.session && ctx.session.user){
      //if(isEmptyObject(req.session.user)){
          ctx.session.user=null;//清除session redis一起清除
          //console.log('clear')
      //}
  }
  ctx.body={errorCode:0};
})
router.get('/user',function(ctx,next){
  var data={errorCode:1};
  //console.log(ctx.session);
  if(ctx.session && ctx.session.user){
      data.errorCode=0;
      data.data=ctx.session.user;
  }
  //console.log(data);
  ctx.body=data;
})
router.post('/user',async function(ctx,next){
  var data={errorCode:1};
  //console.log(ctx.request.header,ctx.session.user);
  if(ctx.session && ctx.session.user){
      data.errorCode=0;
      //data.data=ctx.session.user;
      var s=await Model.findById(ctx.session.user._id);
      //console.log(s);
      delete s.data.pwd;
      data.data=s.data;
      console.log(data);
      ctx.body=data;
  }else{
    ctx.body=data;
  }
  //console.log(data);
  
})
router.post('/create', async function(ctx,next){
  var d=new Date();
  d=tool.format(d,'yyyy-MM-dd hh:mm:ss')
  var req=ctx.request;
  var obj={
      name:req.body.name,
      pwd:req.body.pwd,
      state:req.body.state,
      time:d
  }
  var s=await Model.find({name:obj.name});
  if(s.errorCode==0 && s.data.length>0){
      ctx.body={errorCode:10,message:'此用户名不可用'};
  }else{
      var result=await Model.save(obj);
      ctx.body=result;
  }
  
})
router.post('/update',async function(ctx,next){
  var req=ctx.request;
  var id=req.body._id;
  var obj=req.body;
  delete obj._id;
  var result=await Model.update({_id:id},obj);
  ctx.body=result;
})
router.post('/list',async function(ctx,next){
  var req=ctx.request;
  //console.log(req.body);
  var result=await Model.queryPage({},{page:req.body.currentPage,limit:req.body.pageSize})
  //console.log(result);
  ctx.body=result;
  //ctx.body={data:{list:[]}}
})
router.post('/list/:id',async function(ctx,next){
  
  //console.log(ctx.params)
  var result=await Model.findById(ctx.params.id);
  //console.log(result);
  ctx.body=result;
})
router.post('/remove/:id',async function(ctx,next){
  var req=ctx
  console.log(req.params.id);
  var result=await Model.findOneAndRemove({_id:req.params.id});
  
  ctx.body=result;

})



module.exports = router

